/*
 * Copyright (C) 2013 Guillaume Lesniak
 * Copyright (C) 2012 The Android Open Source Project
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA  02110-1301, USA.
 */

package org.cyanogenmod.focal.ui;

import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.view.View;

import fr.xplod.focal.R;

public class CircleTimerView extends View {


    private int mRedColor;
    private int mWhiteColor;
    private long mIntervalTime = 0;
    private long mIntervalStartTime = -1;
    private long mMarkerTime = -1;
    private long mCurrentIntervalTime = 0;
    private long mAccumulatedTime = 0;
    private boolean mPaused = false;
    private boolean mAnimate = false;
    private static float mCircleXCenterLeftPadding = 0;
    private static float mStrokeSize = 4;
    private static float mDiamondStrokeSize = 12;
    private static float mMarkerStrokeSize = 2;
    private final Paint mPaint = new Paint();
    private final Paint mFill = new Paint();
    private final RectF mArcRect = new RectF();
    private float mRectHalfWidth = 6f;
    private Resources mResources;
    private float mRadiusOffset;   // amount to remove from radius to account for markers on circle
    private float mScreenDensity;

    // Class has 2 modes:
    // Timer mode - counting down. in this mode the animation is counter-clockwise and stops at 0
    // Stop watch mode - counting up - in this mode the animation is clockwise and will keep the
    //                   animation until stopped.
    private boolean mTimerMode = true; // default is timer mode

    public CircleTimerView(Context context) {
        this(context, null);
    }

    public CircleTimerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
        setAlpha(0);
    }

    public void setIntervalTime(long t) {
        mIntervalTime = t;
        postInvalidate();
    }

    public void setMarkerTime(long t) {
        mMarkerTime = t;
        postInvalidate();
    }

    public void reset() {
        mIntervalStartTime = -1;
        mMarkerTime = -1;
        postInvalidate();
    }

    public void startIntervalAnimation() {
        mIntervalStartTime = SystemClock.elapsedRealtime();
        mAnimate = true;
        invalidate();
        mPaused = false;
    }

    public void stopIntervalAnimation() {
        mAnimate = false;
        mIntervalStartTime = -1;
        mAccumulatedTime = 0;
    }

    public boolean isAnimating() {
        return (mIntervalStartTime != -1);
    }

    public void pauseIntervalAnimation() {
        mAnimate = false;
        mAccumulatedTime += SystemClock.elapsedRealtime() - mIntervalStartTime;
        mPaused = true;
    }

    public void abortIntervalAnimation() {
        mAnimate = false;
    }

    public void setPassedTime(long time, boolean drawRed) {
        // The onDraw() method checks if mIntervalStartTime has been set before drawing any red.
        // Without drawRed, mIntervalStartTime should not be set here at all, and would remain at -1
        // when the state is reconfigured after exiting and re-entering the application.
        // If the timer is currently running, this drawRed will not be set, and will have no effect
        // because mIntervalStartTime will be set when the thread next runs.
        // When the timer is not running, mIntervalStartTime will not be set upon loading the state,
        // and no red will be drawn, so drawRed is used to force onDraw() to draw the red portion,
        // despite the timer not running.
        mCurrentIntervalTime = mAccumulatedTime = time;
        if (drawRed) {
            mIntervalStartTime = SystemClock.elapsedRealtime();
        }
        postInvalidate();
    }

    /**
     * Calculate the amount by which the radius of a CircleTimerView should
     * be offset by the any of the extra painted objects.
     */
    public static float calculateRadiusOffset(
            float strokeSize, float diamondStrokeSize, float markerStrokeSize) {
        return Math.max(strokeSize, Math.max(diamondStrokeSize, markerStrokeSize));
    }

    private void init(Context c) {
        mResources = c.getResources();
        mCircleXCenterLeftPadding = (mResources.getDimension(R.dimen.timer_circle_width)
                - mResources.getDimension(R.dimen.timer_circle_diameter)) / 2;
        mStrokeSize = mResources.getDimension(R.dimen.circletimer_circle_size);
        mDiamondStrokeSize = mResources.getDimension(R.dimen.circletimer_diamond_size);
        mMarkerStrokeSize = mResources.getDimension(R.dimen.circletimer_marker_size);
        mRadiusOffset = calculateRadiusOffset(
                mStrokeSize, mDiamondStrokeSize, mMarkerStrokeSize);
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.STROKE);
        mWhiteColor = mResources.getColor(R.color.clock_white);
        mRedColor = mResources.getColor(R.color.clock_red);
        mScreenDensity = mResources.getDisplayMetrics().density;
        mFill.setAntiAlias(true);
        mFill.setStyle(Paint.Style.FILL);
        mFill.setColor(mWhiteColor);
        mRectHalfWidth = mDiamondStrokeSize / 2f;
    }

    public void setTimerMode(boolean mode) {
        mTimerMode = mode;
    }

    @Override
    public void onDraw(Canvas canvas) {
        int xCenter = getWidth() / 2 + 1;
        int yCenter = getHeight() / 2;

        mPaint.setStrokeWidth(mStrokeSize);
        float radius = Math.min(xCenter, yCenter) - mRadiusOffset;

        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
            xCenter = (int) (radius + mRadiusOffset);
            if (mTimerMode) {
                xCenter += mCircleXCenterLeftPadding;
            }
        }

        if (mIntervalStartTime == -1) {
            // Just draw a complete red circle, no white arc needed
            mPaint.setColor(mRedColor);
            canvas.drawCircle (xCenter, yCenter, radius, mPaint);
            if (mTimerMode) {
                drawWhiteDiamond(canvas, 0f, xCenter, yCenter, radius);
            }
        } else {
            if (mAnimate) {
                mCurrentIntervalTime = SystemClock.elapsedRealtime()
                        - mIntervalStartTime + mAccumulatedTime;
            }
            // Draw a combination of red and white arcs to create a circle
            mArcRect.top = yCenter - radius;
            mArcRect.bottom = yCenter + radius;
            mArcRect.left =  xCenter - radius;
            mArcRect.right = xCenter + radius;
            float redPercent = (float)mCurrentIntervalTime / (float)mIntervalTime;
            // Prevent timer from doing more than one full circle
            redPercent = (redPercent > 1 && mTimerMode) ? 1 : redPercent;

            float whitePercent = 1 - (redPercent > 1 ? 1 : redPercent);
            // Draw white arc here
            mPaint.setColor(mWhiteColor);
            if (mTimerMode){
                canvas.drawArc (mArcRect, 270, - redPercent * 360 , false, mPaint);
            } else {
                canvas.drawArc (mArcRect, 270, + redPercent * 360 , false, mPaint);
            }

            // Draw red arc here
            mPaint.setStrokeWidth(mStrokeSize);
            mPaint.setColor(mRedColor);
            if (mTimerMode) {
                canvas.drawArc(mArcRect, 270, + whitePercent * 360, false, mPaint);
            } else {
                canvas.drawArc(mArcRect, 270 + (1 - whitePercent) * 360,
                        whitePercent * 360, false, mPaint);
            }

            if (mMarkerTime != -1 && radius > 0 && mIntervalTime != 0) {
                mPaint.setStrokeWidth(mMarkerStrokeSize);
                float angle = (float)(mMarkerTime % mIntervalTime) / (float)mIntervalTime * 360;
                // Draw 2dips thick marker the formula to draw the marker 1 unit thick is:
                // 180 / (radius * Math.PI) after that we have to scale it by the screen density
                canvas.drawArc (mArcRect, 270 + angle, mScreenDensity *
                        (float) (360 / (radius * Math.PI)) , false, mPaint);
            }
            drawWhiteDiamond(canvas, redPercent, xCenter, yCenter, radius);
        }
        if (mAnimate) {
            invalidate();
        }
    }

    protected void drawWhiteDiamond(
            Canvas canvas, float degrees, int xCenter, int yCenter, float radius) {
        mPaint.setColor(mWhiteColor);
        float diamondPercent;
        if (mTimerMode) {
            diamondPercent = 270 - degrees * 360;
        } else {
            diamondPercent = 270 + degrees * 360;
        }

        canvas.save();
        final double diamondRadians = Math.toRadians(diamondPercent);
        canvas.translate(xCenter + (float) (radius * Math.cos(diamondRadians)),
                yCenter + (float) (radius * Math.sin(diamondRadians)));
        canvas.rotate(diamondPercent + 45f);
        canvas.drawRect(-mRectHalfWidth, -mRectHalfWidth, mRectHalfWidth, mRectHalfWidth, mFill);
        canvas.restore();
    }
}
